 /**
 * Description: 物资借出明细
 * @author:Hunter
 * @since:1.0.0
 *
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2018-04-08   Hunter       1.0.0     Initial
 */
package com.metro.paperless.app.service.impl;

import com.metro.core.dao.IMapper;
import com.metro.core.service.BaseServiceSupport;
import com.metro.core.service.ServiceException;
import com.metro.core.vo.ResultCode;
import com.metro.core.vo.inf.IResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.metro.paperless.app.mapper.IAppLendDetailMapper;
import com.metro.paperless.app.service.inf.IAppLendDetailService;
import com.metro.paperless.biz.beans.vo.BizMaterialsManage;
import com.metro.paperless.biz.mapper.IBizMaterialsManageMapper;
import com.metro.paperless.app.beans.vo.AppLendDetail;

 /**
 * 物资借出明细<br>
 *
 * @author:Hunter
 * @since:1.0.0
 * @version [1.0.0, 2018-04-08]
 */
@Service("applenddetailService")
public class AppLendDetailService extends BaseServiceSupport<AppLendDetail> implements IAppLendDetailService{
    //物资借出明细Mapper
    @Autowired
    private IAppLendDetailMapper applenddetailMapper;
    
    @Autowired
    private IBizMaterialsManageMapper materialsManageMapper;

    @SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
    public IMapper getMapper() {
        return applenddetailMapper;
    }

    @Override
    public IResult save(AppLendDetail entity) {
        try {
            applenddetailMapper.save(entity);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("新增物资借出明细失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult update(AppLendDetail entity) {
        try {
            applenddetailMapper.update(entity);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("修改物资借出明细失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult delete(Long id) {
        try {
            applenddetailMapper.delete(id);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult deleteByIds(String ids) {
        try {
            applenddetailMapper.deleteByIds(ids);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

	@Override
	public IResult deleteInfoById(Long id) {
		try{
			AppLendDetail detail = applenddetailMapper.getById(id);
			BizMaterialsManage materialsManage = materialsManageMapper.getById(detail.getMaterialsId());
			applenddetailMapper.delete(id);
			//改变物资的可用库存
			materialsManage.setEnableStockNum(materialsManage.getEnableStockNum() + detail.getLendNum());
			materialsManageMapper.update(materialsManage);
			return ResultCode.SUCCESS;
		}catch(Exception e){
			log.error("删除物资借出明细并维护物资可用库存", e);
            throw new ServiceException(ResultCode.ERROR);
		}
		
	}
}